package com.easyj.admin.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.easyj.base.common.ajax.Result;
import com.easyj.base.exception.*;




/**
 * 全局异常处理器
 * 
 */
@RestControllerAdvice
public class GlobalExceptionHandler
{
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);



    /**
                *  校验异常
     */
    @ExceptionHandler(ValidateException.class)
    public Result validateException(ValidateException ex){
    	return Result.Error(ex.getCode(),ex.getMessage());
    }

    @ExceptionHandler(UpdateSystemObjectException.class)
    public Result handlerUpdateSystemObjectException(UpdateSystemObjectException ex)
    {
    	return Result.Error(ex.getCode(),ex.getMessage());
    }

    @ExceptionHandler(ActivitiException.class)
    public Result handleTransactionalException(ActivitiException e)
    {
        log.error(e.getMessage(),e);
        return Result.Error(e.getMessage());
    }
    
    @ExceptionHandler(TransactionalException.class)
    public Result handleTransactionalException(AccessDeniedException e)
    {
        log.error(e.getMessage(),e);
        return Result.Error(e.getMessage());
    }

    @ExceptionHandler(ImpValidException.class)
    public Result handleImpExpiredException(ImpValidException ex)
    {
        log.error(ex.getMessage(), ex);
        return Result.Error(ex.getMessage());
    }
    
    @ExceptionHandler(EasyjException.class)
    public Result handlerEasyjExceptionObjectException(EasyjException ex)
    {
    	return Result.Error(ex.getCode(),ex.getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Result handlerNoFoundException(Exception e)
    {
        log.error(e.getMessage(), e);
        return Result.Error(404, "路由不存在。");
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e)
    {
        log.error(e.getMessage(), e);
        return Result.Error(e.getMessage());
    }

    /**
             *    验证异常
     */
    @ExceptionHandler(BindException.class)
    public Result validatedBindException(BindException e)
    {
        
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return Result.Error(message);
    }
    
    /**
            * 验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result validExceptionHandler(MethodArgumentNotValidException e)
    {
        log.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return Result.Error(message);
    }


   

    /**
     * 演示模式异常
    
    @ExceptionHandler(DemoModeException.class)
    public AjaxResult demoModeException(DemoModeException e)
    {
        return AjaxResult.error("演示模式，不允许操作");
    } */
}
